package com.ws.universal.tools.takephoto.multipleimageselect.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.ws.universal.tools.R;
import com.ws.universal.tools.pictureselect.util.MediaUtil;
import com.ws.universal.tools.takephoto.multipleimageselect.models.ImageBean;

import java.util.ArrayList;

/**
 * @author:ws
 * @date: 2020/9/1 14:07
 * @desc
 */
public class CustomImageSelectAdapter extends CustomGenericAdapter<ImageBean> {

    public CustomImageSelectAdapter(Context context, ArrayList<ImageBean> images) {
        super(context, images);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.grid_view_item_image_select, null);

            viewHolder = new ViewHolder();
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image_view_image_select);
            viewHolder.view = convertView.findViewById(R.id.view_alpha);
            viewHolder.ll_video = convertView.findViewById(R.id.ll_video);
            viewHolder.tv_video_time = convertView.findViewById(R.id.tv_video_time);

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (arrayList.get(position).type.equals("video/mp4")) {
            viewHolder.ll_video.setVisibility(View.VISIBLE);
            viewHolder.tv_video_time.setText(MediaUtil.getVideoDuration(arrayList.get(position).duration));

        } else {
            viewHolder.ll_video.setVisibility(View.GONE);
        }
        viewHolder.imageView.getLayoutParams().width = size;
        viewHolder.imageView.getLayoutParams().height = size;

        viewHolder.view.getLayoutParams().width = size;
        viewHolder.view.getLayoutParams().height = size;

        if (arrayList.get(position).isSelected) {
            viewHolder.view.setAlpha(0.5f);
            ((RelativeLayout) convertView).setForeground(context.getResources().getDrawable(R.drawable.ic_done_white));

        } else {
            viewHolder.view.setAlpha(0.0f);
            ((RelativeLayout) convertView).setForeground(null);
        }
        //        if (arrayList.get(position).isSelected) {
        //            viewHolder.view.setAlpha(0.5f);
        //            ((FrameLayout) convertView).setForeground(context.getResources().getDrawable(R.drawable.ic_done_white));
        //
        //        } else {
        //            viewHolder.view.setAlpha(0.0f);
        //            ((FrameLayout) convertView).setForeground(null);
        //        }
        RequestOptions options = new RequestOptions().placeholder(R.drawable.image_placeholder);
        Glide.with(context).load(arrayList.get(position).path).apply(options).into(viewHolder.imageView);

        return convertView;
    }

    private static class ViewHolder {
        public LinearLayout ll_video;
        public TextView tv_video_time;
        public ImageView imageView;
        public View view;
    }
}
